﻿@model IEnumerable<eBusiness.Models.News>

@functions {
    private string GetProductLink(int pageNumber = 1)
    {
        var link = "/Admin/News/Index?";
        
        if (!string.IsNullOrEmpty(ViewBag.CurrentSoftField))
        {
            link += "&softField=" + ViewBag.CurrentSoftField;
        }
        if (!string.IsNullOrEmpty(ViewBag.CurrentSoftDir))
        {
            link += "&softDir=" + ViewBag.CurrentSoftDir;
        }
        if (!string.IsNullOrEmpty(pageNumber.ToString()))
        {
            link += "&pageNumber=" + pageNumber;
        }
        return link;
    }
}

@{
    ViewBag.Title = "Index";
    Layout = "../Shared/_Layout.cshtml";
}

<script type="text/javascript">
    $(document).ready(function () {
        $("#SoftField").change(function (e) {
            window.location.href = '/Admin/News?' + GetParams();
        });
        $("#SoftDir").change(function (e) {
            window.location.href = '/Admin/News?' + GetParams();
        });
    });

    function GetParams() {
        var softField = $("#SoftField :selected").val();
        var softDir = $("#SoftDir :selected").val();
        
        var param = '';
        if (!(softField == '' || softField == null)) {
            param += '&softField=' + softField;
        }
        if (!(softDir == '' || softDir == null)) {
            param += '&softDir=' + softDir;
        }
        return param;
    }
</script>
<h2>
    Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<div class="adminfilterdiv">
    <ul class="productFilter">        
        <li><span>Sort By : </span><span>
            @Html.DropDownList("SoftField", (SelectList)ViewBag.SoftFields, string.Empty)
        </span></li>
        <li><span>Sort Dir : </span><span>
            @Html.DropDownList("SoftDir", (SelectList)ViewBag.SoftDir, string.Empty)
        </span></li>
    </ul>
</div>
<table class="dashboardTable">
    <tr>
        <th style="width: 80px;">
            ThumbImage
        </th>
        <th style="width: 200px;">
            Title
        </th>
        <th style="width: 550px;">
            Details
        </th>
        <th  style="width: 120px;">
            CreatedDate
        </th>
        <th  style="width: 80px;">
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                <img src="@item.ThumbImage" alt="image" class="thumbImage" />
            </td>
            <td>
                <a href="@Url.Action("Details", "News", new { id = @item.ID })">@Html.DisplayFor(modelItem => item.Title)</a>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Details)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreatedDate)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.ID })
            </td>
        </tr>
    }
</table>
<div class="common_paging">
    <ul class="nav_common_ul">
        @{
            if (ViewBag.CurrentPage > 1)
            {
                
            <li><a href="@GetProductLink()" class="common_paging_first">First</a></li>
            <li><a href="@GetProductLink((int)ViewBag.CurrentPage - 1)" class="common_paging_previous">
                Prev</a></li>
            }
            <li>page
                <input id="goToPage" type="text" class="common_pagging_page" style="width: 50px" value="@ViewBag.CurrentPage"/>
                of @ViewBag.NumberOfPage
            </li>
            if (ViewBag.CurrentPage < ViewBag.NumberOfPage)
            {
            <li><a href="@GetProductLink((int)ViewBag.CurrentPage + 1)" class="common_paging_next">
                Next</a></li>
            <li><a href="@GetProductLink((int)ViewBag.NumberOfPage)" class="common_paging_last">
                Last</a></li>
            }
        }
    </ul>
</div>